package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 * The Class TtlsbeooDataOnDemand.
 */
@Configurable
@Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
@Transactional
@RooDataOnDemand(entity = Ttlsbeoo.class)
public class TtlsbeooDataOnDemand {
	
	
	/**
	 * Test get.
	 */
	@Test
	public void testGet()
	{
		Ttlsbeoo oo= new Ttlsbeoo();
		TtlsbeooPK id= new TtlsbeooPK("GMS","013");
		
		oo.setId(id);
		oo.persist();
		
		oo= Ttlsbeoo.findTtlsbeoo(id);
		
		System.out.println(oo.getAudUsuario());
		System.out.println(oo.getDesCampolar());
		System.out.println(oo.getDesCampoord());
		System.out.println("CLASE "+oo.getId().getCodCampoord());
		System.out.println("TIPO "+oo.getId().getCodTipord());
		System.out.println("TO_STRING "+oo.getId().toString());
		System.out.println("KAKA"+oo.toString());
	}

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbeoo> data;

	/**
	 * Gets the new transient ttlsbeoo.
	 *
	 * @param index the index
	 * @return the new transient ttlsbeoo
	 */
	public Ttlsbeoo getNewTransientTtlsbeoo(int index) {
        Ttlsbeoo obj = new Ttlsbeoo();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodSepa(obj, index);
        setDesCampolar(obj, index);
        setDesCampoord(obj, index);
        setDesColeccio(obj, index);
        setDesEncadena(obj, index);
        setDesValopci(obj, index);
        setXsnCampopci(obj, index);
        setXsnEncadena(obj, index);
        setXsnOculto(obj, index);
        setXtiTipocam(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbeoo obj, int index) {
        TtlsbeooPK embeddedIdClass = new TtlsbeooPK("co_" + index, "c_" + index);
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbeoo obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbeoo obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod sepa.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodSepa(Ttlsbeoo obj, int index) {
        Character codSepa = new Character('N');
        obj.setCodSepa(codSepa);
    }

	/**
	 * Sets the des campolar.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesCampolar(Ttlsbeoo obj, int index) {
        String desCampolar = "desCampolar_" + index;
        if (desCampolar.length() > 40) {
            desCampolar = desCampolar.substring(0, 40);
        }
        obj.setDesCampolar(desCampolar);
    }

	/**
	 * Sets the des campoord.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesCampoord(Ttlsbeoo obj, int index) {
        String desCampoord = "desCampoord_" + index;
        if (desCampoord.length() > 100) {
            desCampoord = desCampoord.substring(0, 100);
        }
        obj.setDesCampoord(desCampoord);
    }

	/**
	 * Sets the des coleccio.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesColeccio(Ttlsbeoo obj, int index) {
        String desColeccio = "desColeccio_" + index;
        if (desColeccio.length() > 60) {
            desColeccio = desColeccio.substring(0, 60);
        }
        obj.setDesColeccio(desColeccio);
    }

	/**
	 * Sets the des encadena.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesEncadena(Ttlsbeoo obj, int index) {
        Character desEncadena = new Character('N');
        obj.setDesEncadena(desEncadena);
    }

	/**
	 * Sets the des valopci.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesValopci(Ttlsbeoo obj, int index) {
        String desValopci = "desValopci_" + index;
        if (desValopci.length() > 50) {
            desValopci = desValopci.substring(0, 50);
        }
        obj.setDesValopci(desValopci);
    }

	/**
	 * Sets the xsn campopci.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnCampopci(Ttlsbeoo obj, int index) {
        Character xsnCampopci = new Character('N');
        obj.setXsnCampopci(xsnCampopci);
    }

	/**
	 * Sets the xsn encadena.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnEncadena(Ttlsbeoo obj, int index) {
        Character xsnEncadena = new Character('N');
        obj.setXsnEncadena(xsnEncadena);
    }

	/**
	 * Sets the xsn oculto.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnOculto(Ttlsbeoo obj, int index) {
        BigDecimal xsnOculto = BigDecimal.valueOf(index);
        obj.setXsnOculto(xsnOculto);
    }

	/**
	 * Sets the xti tipocam.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXtiTipocam(Ttlsbeoo obj, int index) {
        Character xtiTipocam = new Character('N');
        obj.setXtiTipocam(xtiTipocam);
    }

	/**
	 * Gets the specific ttlsbeoo.
	 *
	 * @param index the index
	 * @return the specific ttlsbeoo
	 */
	public Ttlsbeoo getSpecificTtlsbeoo(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbeoo obj = data.get(index);
        return Ttlsbeoo.findTtlsbeoo(obj.getId());
    }

	/**
	 * Gets the random ttlsbeoo.
	 *
	 * @return the random ttlsbeoo
	 */
	public Ttlsbeoo getRandomTtlsbeoo() {
        init();
        Ttlsbeoo obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbeoo.findTtlsbeoo(obj.getId());
    }

	/**
	 * Modify ttlsbeoo.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbeoo(Ttlsbeoo obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbeoo.findTtlsbeooEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbeoo' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Ttlsbeoo>();
        for (int i = 0; i < 10; i++) {
            Ttlsbeoo obj = getNewTransientTtlsbeoo(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
